package com.zhoug.common3.utils;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.view.ViewCompat;
import androidx.core.widget.ImageViewCompat;
import androidx.core.widget.TextViewCompat;

/**
 * @Author: zhoug
 * @Date: 2024-07-12
 * @Description:
 */
public class AttributeUtils {
    /**
     * 设置文字的最大行数
     *
     * @param textView
     * @param maxLines
     */
    public static void setMaxLines(TextView textView, int maxLines) {
        if (textView == null) return;
        if (maxLines > 0) {
            textView.setMaxLines(maxLines);
            textView.setSingleLine(maxLines == 1);
        }
    }

    /**
     * 设置文字的样式
     *
     * @param textView
     * @param typeFace {@link Typeface#BOLD}...
     */
    public static void setTypeface(TextView textView, int typeFace) {
        if (textView == null) return;
        if (typeFace >= Typeface.NORMAL && typeFace <= Typeface.BOLD_ITALIC) {
            textView.setTypeface(Typeface.defaultFromStyle(typeFace));
        } else {
            textView.setTypeface(null);
        }
    }

    /**
     * 设置组件狂傲
     *
     * @param view
     * @param width  宽
     * @param height 高
     */
    public static void setSize(View view, int width, int height) {
        if (view == null) return;
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams != null) {
            layoutParams.width = width;
            layoutParams.height = height;
            view.setLayoutParams(layoutParams);
        }
    }

    /**
     * 设置组件狂傲
     *
     * @param view
     * @param width  宽
     * @param height 高
     */
    public static void setSizeExcludeZero(View view, int width, int height) {
        if (view == null) return;
        if (width != 0 || height != 0) {
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            if (layoutParams != null) {
                if (width != 0) {
                    layoutParams.width = width;
                }
                if (height != 0) {
                    layoutParams.height = height;
                }
                view.setLayoutParams(layoutParams);
            }
        }
    }

    /**
     * 设置文字颜色
     *
     * @param textView
     * @param color        颜色
     * @param excludeColor 排除的颜色
     */
    public static void setTextColor(TextView textView, int color, int excludeColor) {
        if (color != excludeColor) {
            setTextColor(textView, color);
        }
    }

    /**
     * 设置文字颜色
     *
     * @param textView
     * @param color    颜色
     */
    public static void setTextColor(TextView textView, int color) {
        if (textView == null) return;
        textView.setTextColor(color);
    }

    /**
     * 设置文字大小
     *
     * @param textView
     * @param textSize    文字大小 px
     * @param excludeSize 排除的大小
     */
    public static void setTextSize(TextView textView, float textSize, float excludeSize) {
        if (textSize != excludeSize) {
            setTextSize(textView, textSize);
        }
    }

    /**
     * 设置文字大小
     *
     * @param textView
     * @param textSize 文字大小 px
     */
    public static void setTextSize(TextView textView, float textSize) {
        setTextSizeWithUnit(textView, TypedValue.COMPLEX_UNIT_PX, textSize);
    }

    /**
     * 设置文字大小
     *
     * @param textView
     * @param unit     单位{@link TypedValue#COMPLEX_UNIT_DIP}...
     * @param textSize 文字大小
     */
    public static void setTextSizeWithUnit(TextView textView, int unit, float textSize) {
        if (textView == null) return;
        textView.setTextSize(unit, textSize);
    }

    /**
     * TextView水平和垂直缩放文本大小以适应容器
     *
     * @param textView
     * @param autoSizeMinTextSize     最小文字大小
     * @param autoSizeMaxTextSize     最大文字大小
     * @param autoSizeStepGranularity 每次改变的增量
     * @param unit                    单位{@link TypedValue#COMPLEX_UNIT_PX} ...
     */
    public static void setAutoTextSize(TextView textView, int autoSizeMinTextSize,
                                       int autoSizeMaxTextSize,
                                       int autoSizeStepGranularity,
                                       int unit) {
        TextViewCompat.setAutoSizeTextTypeWithDefaults(textView, TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
        TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(textView,
                autoSizeMinTextSize, autoSizeMaxTextSize, autoSizeStepGranularity, unit);
    }

    /**
     * 转化为px
     *
     * @param context
     * @param unit    单位{@link TypedValue#COMPLEX_UNIT_DIP}...
     * @param size
     * @return
     */
    public static float applyDimension(Context context, int unit, int size) {
        return ResourceUtils.applyDimension(context, unit, size);
    }


    public static void setTint(Drawable drawable, int tint) {
        if (drawable == null) return;
        DrawableCompat.setTint(drawable, tint);
    }

    public static void setTintList(Drawable drawable, ColorStateList colorStateList) {
        if (drawable == null) return;
        DrawableCompat.setTintList(drawable, colorStateList);
    }

    public static Drawable getDrawable(Context context, @DrawableRes int drawableRes, @ColorRes int tintColorRes) {
        if (context == null) return null;
        Drawable drawable = ContextCompat.getDrawable(context, drawableRes);
        if (drawable != null && tintColorRes != 0) {
            DrawableCompat.setTintList(drawable, AppCompatResources.getColorStateList(context, tintColorRes));
        }
        return drawable;
    }


    public static void setImageTintList(ImageView imageView, ColorStateList colorStateList) {
        if (imageView == null) return;
        if (colorStateList != null) {
            ImageViewCompat.setImageTintList(imageView, colorStateList);
        }

    }

    public static void setBackgroundTintList(View view, ColorStateList colorStateList) {
        if (view == null) return;
        ViewCompat.setBackgroundTintList(view, colorStateList);

    }

    public static @Nullable TypedArray getStyle(Context context, TypedArray typedArray, int index, int[] attrs) {
        TypedValue typedValue = new TypedValue();
        boolean success = typedArray.getValue(index, typedValue);
        if (success) {
            if (typedValue.type == TypedValue.TYPE_REFERENCE) {
                return context.obtainStyledAttributes(typedValue.data, attrs);
            }
        }

//        int resourceId = typedArray.getResourceId(index, 0);
//        if (resourceId != 0) {
//            return context.getTheme().obtainStyledAttributes(resourceId, attrs);
//        }
        return null;
    }

}
